import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  dictList: {},
  currentDict: {},
  nodeList: [],
  currentNodeDict: {}
};

const getters = {
  dictList: state => state.dictList,
  currentDict: state => state.currentDict,
  currentNodeDict: state => state.currentNodeDict,
  nodeList: state => state.nodeList
};

const actions = {
  //数据字典列表
  dataListUsingPOSTS({ commit }, param) {
    return Api.getSystemDictionaryPageList(param).then(ret => {
      commit(types.UPDATE_DICT_LIST, ret.data);
      return ret;
    });
  },
  //数据字典详情
  viewUsingPOSTS({ commit }, param) {
    return Api.getSystemDictionaryById(param).then(ret => {
      commit(types.UPDATE_CURRENT_DICT, ret.data);
      return ret;
    });
  },
  dataSaveUsingPOSTS({ commit }, param) {
    return Api.dataSaveUsingPOST(param).then(ret => {
      commit(types.INIT_CURRENT_DICT, ret.data);
      return ret;
    });
  },
  //数据字典新增/修改
  addOrUpdateSystemDictionary({ commit }, param) {
    return Api.addOrUpdateSystemDictionary(param).then(ret => {
      commit(types.INIT_CURRENT_DICT, ret.data);
      return ret;
    });
  },
  //数据字典删除
  delListUsingPOSTS({ commit }, param) {
    return Api.deleteSystemDictionaryById(param).then(ret => {
      return ret;
    });
  },
  //  数据字典选项添加
  dataSaveNodeUsingPOSTS({ commit }, param) {
    return Api.dataSaveNodeUsingPOST(param).then(ret => {
      commit(types.INIT_CURRENT_NODE_DICT, ret.data);
      return ret;
    });
  },
  // 数据字典列表
  dataListSubUsingPOSTS({ commit }, param) {
    return Api.dataListSubUsingPOST(param).then(ret => {
      commit(types.UPDATE_NODE_LIST, ret.data);
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_DICT_LIST](state, payload) {
    state.dictList = payload;
  },
  [types.UPDATE_NODE_LIST](state, payload) {
    state.nodeList = payload;
  },

  [types.UPDATE_CURRENT_DICT](state, payload) {
    state.currentDict = payload;
  },

  [types.INIT_CURRENT_DICT](state) {
    state.currentDict = {
      keyword: "",
      name: "",
      status: "ENABLE",
      parentId: "0",
      value: ""
    };
  },
  [types.INIT_CURRENT_NODE_DICT](state) {
    state.currentNodeDict = {
      keyword: "",
      name: "",
      status: "ENABLE",
      parentId: "0",
      value: ""
    };
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
